<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

JFX::checkAdminPermsRedirect('core', 'perm_manage_languages');

$THEME->addBreadcrumb($this->adminUrl.'/lang/translate/', $this->lang('ap_title_lang_translate'));


$langid = $DB->escape(get('lang'));
$moduleid = $DB->escape(get('module'));

    $VIEW->addJs('
var JFXTranslateCurrentEditingId = \'\';
var JFXTranslateCurrentLang = "'.$langid.'";
$(document).ready(function(){
    var editableFunction = function(value, settings){
        var id = JFXTranslateCurrentEditingId.split("_");
        var desc = "";
        var val = "";
        if(id[0] == "value") var val = value;
        else if(id[0] == "desc") var desc = value;
        value = value.replace(/^\s+|\s+$/g, \'\');
        var el = $("#"+JFXTranslateCurrentEditingId).get(0);
        var parentEl = el.parentNode;
        var moduleKey = $(".jfx-module-key", parentEl).text();
        var label = $(".jfx-label", parentEl).text();
        var jsonParams = {
            module_key : moduleKey,
            label : label,
            lang : JFXTranslateCurrentLang,
            value : val,
            desc : desc
        }
        $.post("'.$CONFIG->adminUrl.'/ajax/update-system-lang", jsonParams, function(){return;});
        return value;
    };

    var editableSettings = {
        "type" : "textarea",
        "submit" : "OK",
        "id" : this.id
    };
    $(".jfx-editable-content").mousedown(function(){
        JFXTranslateCurrentEditingId = this.id;
    });
    $(".jfx-editable-content").editable(editableFunction, editableSettings);

    $("#jfx-lang-select").change(function(){
		$.post("'.$CONFIG->adminUrl.'/lang/translate", {module : \''.$moduleid.'\', lang : this.options[this.selectedIndex].value, \'JFXGetAjaxContent\' : 1}, JFXParseAjaxContent);
    });
    $("#jfx-module-select").change(function(){
		$.post("'.$CONFIG->adminUrl.'/lang/translate", {module : this.options[this.selectedIndex].value, lang : \''.$langid.'\', "JFXGetAjaxContent" : 1}, JFXParseAjaxContent);
    });

});
');


$langs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}languages ORDER BY keyname ASC");

$langOpts = array('0' => $this->lang('select_a_language'));
foreach($langs as $k=>$v){
    $langOpts[$v['keyname']] = $v['keyname'];
};

$langSelect = '<select id="jfx-lang-select">';
foreach($langOpts as $k=>$v){
    if($k==$langid) $selected = 'selected="selected"';
    else $selected = '';
    $langSelect .= '<option value="'.$k.'" '.$selected.'>'.$v.'</option>';
}
$langSelect .= '</select>';

$modules = JFX_Module::getInstalledModules();
$moduleOpts = array('0' => $this->lang('select_a_module'));
foreach($modules as $k=>$v){
    $moduleOpts[$v['keyname']] = $v['title'];
}
$moduleSelect = '<select id="jfx-module-select">';
foreach($moduleOpts as $k=>$v){
    if($k==$moduleid) $selected = 'selected="selected"';
    else $selected = '';
    $moduleSelect .= '<option value="'.$k.'" '.$selected.'>'.$v.'</option>';
}
$moduleSelect .= '</select>';

echo $langSelect.$moduleSelect;



if($DB->countRows($CONFIG->dbprefix.'languages', "keyname = '{$langid}'")>0){
    // valid language

    $langs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}lang_system WHERE lang = '{$langid}' AND module_key = '{$moduleid}' ORDER BY module_key ASC, label ASC");

    $SMARTY->assign('langs', $langs);
    $SMARTY->assign('langid', $langid);
    $content = $SMARTY->fetch($CONFIG->templatesDir.'/admin/lang/translate.tpl');

    echo $content;

}

echo '<div class="jfx-clear"></div>';


